//
//  LoreViewController.m
//  项目三
//
//  Created by rimi1 on 2016/12/9.
//  Copyright © 2016年 Wangxingqian. All rights reserved.
//

#import "LoreViewController.h"
#import "LoreDetailViewController.h"

#import "LoreTableViewCell.h"

#import "NetRequest+Lore.h"

@interface LoreViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, assign) NSInteger page;

@end

@implementation LoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeAppearance];
}

#pragma mark - initialize

- (void)initializeDataSource {
    self.page = 1;
    self.dataArr = [NSMutableArray array];
    [self urlRequestList];
}

- (void)initializeAppearance {
    [self.view addSubview:self.tableView];
}

#pragma mark - URLRequest

- (void)urlRequestList {
    NSDictionary *parameter = @{@"id":@"3", @"page":[NSString stringWithFormat:@"%ld", self.page], @"rows":@"15"};
    [NetRequest getLoreListRequestWithParameter:parameter succeed:^(id response) {
        if ([response[@"status"] integerValue] == 1) {
            [self.dataArr addObjectsFromArray:[LoreModel modelArrForArray:response[@"tngou"]]];
            [self.tableView reloadData];
        }else {
            [Alert alertWithTitle:@"请求错误" message:nil];
        }
        [self.tableView.mj_footer endRefreshing];
        
    } failure:^(NSError *error) {
        [self.tableView.mj_footer endRefreshing];
        self.page--;
        [Alert alertWithTitle:@"请求错误" message:nil];
    }];
}

- (void)upPullLoad {
    self.page++;
    [self urlRequestList];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reusing = @"LoreCell";
    LoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusing];
    if (!cell) {
        cell = [[LoreTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusing];
    }
    
    cell.model = self.dataArr[indexPath.row];
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    LoreModel *model = self.dataArr[indexPath.row];
    LoreDetailViewController *detail = [[LoreDetailViewController alloc] init];
    detail.loreId = model.loreId;
    detail.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detail animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return WXQAdaptionHeight(80) + 20;
}

#pragma mark - get

- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWIDTH, kHEIGHT - 64 - 49) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upPullLoad)];
        _tableView.backgroundColor = kCOLOR_RGB(236, 236, 236, 1);
        
    }
    return _tableView;
}


@end









